﻿using DatabaseAccess;
using Liguo.AppCode;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace Liguo.Store
{
    public partial class Cart : System.Web.UI.Page
    {
        
        SqlIt sql = new SqlIt();
        //DataSet类承接查询结果
        DataSet ds = new DataSet();
        int userid=140;
        double totalprice = 0;
        Label g = new Label();
        int number = 0;
        int flag = 0;         //标记总价格要不要显示，如果为0表示显示，如果为1 显示为待定
        protected void Page_Load(object sender, EventArgs e)
        {
            //HtmlTable TABLE1=new HtmlTable();            //2
            if (Session["User"] != null)
            {
                User user = (User)Session["User"];
                userid = user.UserID;
                
            }
            else {
                Response.Redirect("Port.aspx");
            }
            show();

            //t();
        }
        protected void show()
        {
            string feild = "GoodsName,CartInfo.ID,CartInfo.GoodsID,Price,CartInfo.Amount,CartInfo.Weight,PicturePath,SaleMethod";
            string table = "GoodsInfo,CartInfo";
            string condition = "CartInfo.UserID='" + userid + "' and CartInfo.GoodsID=GoodsInfo.ID";
            ds = sql.SpecialItem(table, condition, feild);
            number = ds.Tables[0].Rows.Count;
            BuildTable(ds);
        }
        private void t()
        {
            for (int i = 0; i < 5; i++)
            {
                HtmlTableRow tr1 = new HtmlTableRow();//定义行
                HtmlTableCell td1 = new HtmlTableCell();//定义商品信息列
                Button b1 = new Button();
                b1.Click += abutton_click;
                b1.Text = "t";
                b1.ID = "but" + i;
                //HyperLink hyperlink = new HyperLink();
                string path = "../images/26.png";
                Image imageb = new Image();      //图片

                imageb.Attributes["src"] = path;
                Label show = new Label();
                show.Text = "0";
                show.ID = "show" + i;
                td1.Controls.Add(b1);
                td1.Controls.Add(show);
                td1.Controls.Add(imageb);
                tr1.Cells.Add(td1);
                MyTable.Rows.Add(tr1);
            }

        }
        private void BuildTable(DataSet ds)
        {
            int n = ds.Tables[0].Rows.Count;
            //number = n;
            //定义表格头部
            HtmlTableRow tr = new HtmlTableRow();//定义行
            HtmlTableCell htd = new HtmlTableCell();//定义商品信息列
            Label lb1 = new Label();
            lb1.Text = "商品";
            htd.Controls.Add(lb1);
            htd.Width = "50";
            HtmlTableCell htd1 = new HtmlTableCell();//定义商品信息列
            Label lb2 = new Label();
            lb2.Text = "单价";
            htd1.Controls.Add(lb2);
            htd1.Width = "50";
            HtmlTableCell htdw = new HtmlTableCell();//定义商品信息列
            Label lbdw = new Label();
            lbdw.Text = "单位";
            htdw.Controls.Add(lbdw);
            htdw.Width = "40";
            HtmlTableCell htd2 = new HtmlTableCell();//定义商品信息列
            Label lb3 = new Label();
            lb3.Text = "数量/个数";
            htd2.Controls.Add(lb3);
            htd2.Width = "130";
            htd2.Align = "center";
            HtmlTableCell htd3 = new HtmlTableCell();//定义商品信息列
            Label lb4 = new Label();
            lb4.Text = "小计";
            htd3.Controls.Add(lb4);
            htd3.Width = "100";
            HtmlTableCell htd4 = new HtmlTableCell();//定义商品信息列
            Label lb5 = new Label();
            lb5.Text = "操作";
            htd4.Controls.Add(lb5);
            htd4.Width = "100";
            HtmlTableCell htd5 = new HtmlTableCell();//定义商品信息列
            Label lb6 = new Label();
            lb6.Text = "  ";
            htd4.Controls.Add(lb6);
            htd5.Width = "100";
            tr.Cells.Add(htd5);
            tr.Cells.Add(htd);
            tr.Cells.Add(htd1);
            tr.Cells.Add(htdw);
            tr.Cells.Add(htd2);
            tr.Cells.Add(htd3);
            tr.Cells.Add(htd4);
            tr.BgColor ="#f7f7f7";
            tr.Attributes["class"] = "cart_inner";
            MyTable.Rows.Add(tr);
            if (n == 0)
            {
                //HtmlTableRow trn = new HtmlTableRow();//定义行
                //HtmlTableCell tdn = new HtmlTableCell();//定义商品信息列
                //Label show = new Label();
                //show.Text = "还没有选择商品哦~";
                //tdn.Controls.Add(show);
                //tdn.ColSpan = 3;
                //trn.Cells.Add(tdn);
                //MyTable.Rows.Add(trn);
                MyTable.Visible = false;
                notify.Visible = true;
            }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    int judge = (int)dr["SaleMethod"];
                    //if (judge==0)   //假设是论个数的
                    {
                        HtmlTableRow tr1 = new HtmlTableRow();//定义行
                        tr1.ID = "row" + i;
                        HtmlTableCell td1 = new HtmlTableCell();//定义商品信息列
                        Label goodsid = new Label();
                        goodsid.Text = "" + dr["GoodsID"];
                        goodsid.ID = "gid" + i;
                        goodsid.Visible = false;
                        Label cartid = new Label();
                        cartid.Text = "" + dr["ID"];
                        cartid.ID = "cartid" + i;
                        cartid.Visible = false;
                        goodsid.Visible = false;
                        Label salemethod = new Label();
                        salemethod.Text = "" + judge;
                        salemethod.ID = "sm" + i;
                        salemethod.Visible = false;
                        ImageButton imageb = new ImageButton();      //图片
                        string path = "../images/" + dr["PicturePath"] + ".png";
                        imageb.Attributes["src"] = path;
                        imageb.Attributes["width"] = "50";
                        imageb.Attributes["height"] = "50";
                        imageb.Attributes.Add("onclick", "this.form.target='_blank'");  //在新页面中打开
                        imageb.ID = "imageb" + i;
                        imageb.Click += ImageButton_Click;
                        td1.Controls.Add(cartid);
                        td1.Controls.Add(goodsid);
                        td1.Controls.Add(salemethod);
                        td1.Controls.Add(imageb);
                        td1.Width = "50";
                        td1.Align = "center";
                        //td1.BgColor = "lightblue";
                        HtmlTableCell td2 = new HtmlTableCell();
                        LinkButton linkb = new LinkButton();         //商品名
                        linkb.Text = (string)dr["GoodsName"];
                        linkb.ID = "goodsname" + i;
                        linkb.Attributes.Add("onclick", "this.form.target='_blank'");
                        linkb.Click += new System.EventHandler(lb_click);
                        td2.Controls.Add(linkb);
                        td2.Width = "100";
                        HtmlTableCell td3 = new HtmlTableCell();//定义列
                        Label lb = new Label();
                        string temp = dr["Price"].ToString();
                        lb.Text = "" + this.formatTrans(Double.Parse(temp));
                        lb.ID = "price" + i;
                        lb.Attributes["class"] = "singleprice";
                        td3.Controls.Add(lb);//列中添加txt1
                        td3.Width = "100";
                        HtmlTableCell htwd = new HtmlTableCell();//定义商品信息列
                        Label lbd = new Label();
                        if (judge == 0)
                        {
                            lbd.Text = "/件";
                        }
                        else if (judge == 2)
                        {
                            lbd.Text = "/千克";
                        }
                        else {
                            lbd.Text = "/件";
                        }
                        lbd.Attributes["class"] = "singleprice";
                        htwd.Controls.Add(lbd);
                        htwd.Align = "left";
                        htwd.Width = "20";
                        HtmlTableCell td4 = new HtmlTableCell();
                        Button button1 = new Button();
                        button1.Text = "-";
                        button1.ID = "minus" + i;
                        //button1.Attributes["OnClick"] = "return minus" + i + "()";
                        button1.Click += mbutton_click;
                        TextBox tb = new TextBox();
                        tb.ID = "amount" + i;
                         if (judge == 2)  //散卖散算就读去重量
                        {
                            tb.Text = "" + dr["Weight"];
                        }
                        else
                        {
                            tb.Text = "" + dr["Amount"];
                        }
                                 //个数
                        tb.Width = 20;
                        tb.ReadOnly = true;
                        Button button2 = new Button();
                        button2.Text = "+";
                        button2.ID = "add" + i;
                        //button1.Attributes["OnClick"] = "return add" + i + "()";
                        button2.Click += abutton_click;
                        Label dw = new Label();
                        if (judge == 0)
                        {
                            dw.Text = "件";
                        }else if(judge==1){
                            dw.Text = "个";
                        }
                        else if (judge == 2)
                        {
                            dw.Text = "千克";
                        }
                        else
                        {
                            dw.Text = "件";
                        }
                        dw.Attributes["class"] = "singleprice";
                        td4.Controls.Add(button1);
                        td4.Controls.Add(tb);
                        td4.Controls.Add(button2);
                        td4.Controls.Add(dw);
                        td4.Width = "100";
                        td4.Align = "center";
                        HtmlTableCell td5 = new HtmlTableCell();//定义列
                        Label suma = new Label();
                        double tp = 0; 
                        if (judge == 1)           //如果整卖散算则价格待定，需根据具体重量
                        {
                            suma.Text = "待定";
                            flag += 1;            //有一个加1
                        }
                        else
                        {
                            tp = (Double.Parse(tb.Text) * Double.Parse(lb.Text));
                            suma.Text = "" + tp;   //小记价格
                            totalprice += tp;
                        }
                        suma.ID = "sum" + i;                       
                        td5.Controls.Add(suma);
                        td5.Width = "100";
                        td5.Attributes["class"] = "price";
                        HtmlTableCell td6 = new HtmlTableCell();//定义列
                        td6.ID = "cell" + i;
                        LinkButton delete = new LinkButton();         //商品名
                        delete.Text = "删除";
                        delete.ID = "delete" + i;
                        delete.OnClientClick = "return confirm('你确认删除?')";
                       //delete.OnClientClick = "return verifydelete()";
                       delete.Click += new System.EventHandler(delete_click);
                       
                        td6.Controls.Add(delete);
                        td6.Width = "100";
                        
                        tr1.Cells.Add(td1);//行中添加列td1
                        tr1.Cells.Add(td2);//行中添加列td1
                        tr1.Cells.Add(td3);
                        tr1.Cells.Add(htwd);
                        tr1.Cells.Add(td4);
                        tr1.Cells.Add(td5);
                        tr1.Cells.Add(td6);
                        //tr1.BgColor = "PapayaWhip";
                        tr1.Attributes["class"] = "cart_body";
                        MyTable.Rows.Add(tr1);//表中添加行

                    }
                }
            }
            HtmlTableRow trt = new HtmlTableRow();//定义行
            trt.ID = "tail";
            HtmlTableCell tdt = new HtmlTableCell();//定义商品信息列
            Label total = new Label();
            total.Text = "合计：￥ ";
            //.ID = "tp";
            total.Attributes["class"] = "tail1";
            tdt.Controls.Add(total);
            tdt.ColSpan = 5;
            tdt.Align = "right";
            HtmlTableCell tdp = new HtmlTableCell();//定义商品信息列
            g.Text = totalprice + "";   
            g.CssClass = "tail2";
            Label dd = new Label();
            if (flag == 0) { 
              dd.Text="";
            }else{
                dd.Text = "+待定";
            }
            dd.CssClass = "tail2";
            dd.ID = "daiding";
            tdt.Controls.Add(g);
            tdt.Controls.Add(dd);
            HtmlTableCell tdb = new HtmlTableCell();
            Button b = new Button();
            b.Text = "结算";
            b.CssClass = "submit_button";
            b.Click += submit_click;
            if (n == 0) {
                b.Enabled =false;
            }
            tdb.Controls.Add(b);
            trt.Cells.Add(tdt);
            trt.Cells.Add(tdp);
            trt.Cells.Add(tdb);
           // trt.BgColor = "lightgrey";
            trt.Attributes["class"] = "cart_body";
            MyTable.Rows.Add(trt);
        }
        protected void mbutton_click(object sender, EventArgs e)
        {
            try
            {             
                Button te = (Button)sender;
                Label labelid = te.Parent.FindControl(te.ID.Replace("minus", "gid")) as Label;
                string sgoodsid = labelid.Text;
                string addcond = " UserID='" + userid + "' and  GoodsID='" + sgoodsid + "'";

                TextBox textbox = te.Parent.FindControl(te.ID.Replace("minus", "amount")) as TextBox;  //3 关联，可惜现在在服务端
                LinkButton lb = te.Parent.FindControl(te.ID.Replace("minus", "delete")) as LinkButton;
                if (textbox.Text == null)
                {
                    //delete_click(lb, e);
                    return;
                }
                 Label sale = te.Parent.FindControl(te.ID.Replace("minus", "sm")) as Label;
                 if (sale.Text == "1") {                 //整卖散买
                     double a = Double.Parse(textbox.Text) - 1;
                     
                     if (a <= 0) {
                         (te.Parent.FindControl("daiding") as Label).Text = "";
                     }
                     if (a + 1 <= 1) {
                         //delete_click(sender, e);
                         Response.Write("<script>alert('如果需要删除商品请点击删除')</script>");
                         return;
                     }
                     textbox.Text = a + "";
                     Dictionary<string, string> info = new Dictionary<string, string>();
                     info["Amount"] = textbox.Text;
                     sql.UpdateMess("CartInfo", info, addcond);
                 }
                 else if (sale.Text == "0") {           //整卖整买
                     double a = Double.Parse(textbox.Text) - 1;
                     if (a + 1 <= 1)
                     {
                         Response.Write("<script>alert('如果需要删除商品请点击删除')</script>");
                         return;
                     }
                     textbox.Text = a + "";
                     Dictionary<string, string> info = new Dictionary<string, string>();
                     info["Amount"] = textbox.Text;
                     sql.UpdateMess("CartInfo", info, addcond);
                     Label label2 = te.Parent.FindControl(te.ID.Replace("minus", "price")) as Label;
                     if (label2.Text == null)
                     {
                         return;
                     }
                     double p = Double.Parse(label2.Text);
                     Label label3 = te.Parent.FindControl(te.ID.Replace("minus", "sum")) as Label;
                     label3.Text = a * p + "";
                     totalprice = Double.Parse(g.Text) - p;
                     g.Text = "" + totalprice;
                 }
                 else if (sale.Text == "2") {         //散卖散买
                     double a = Double.Parse(textbox.Text) - 0.1;
                     if (a + 0.1 <= 0.1)
                     {
                         Response.Write("<script>alert('如果需要删除商品请点击删除')</script>");
                         return;
                     }
                     textbox.Text = a + "";
                     Dictionary<string, string> info = new Dictionary<string, string>();
                     info["Weight"] = textbox.Text;
                     sql.UpdateMess("CartInfo", info, addcond);
                     Label label2 = te.Parent.FindControl(te.ID.Replace("minus", "price")) as Label;
                     if (label2.Text == null)
                     {
                         return;
                     }
                     double p = Double.Parse(label2.Text);
                     Label label3 = te.Parent.FindControl(te.ID.Replace("minus", "sum")) as Label;
                     totalprice = Double.Parse(g.Text) - Double.Parse(label3.Text)+0.000001;
                     Double tempsum = formatTrans(a * p);
                     label3.Text = tempsum+ "";
                     totalprice +=tempsum;
                     g.Text = "" + this.formatTrans(totalprice);
                 }
                
               
            }
            catch (NullReferenceException ex)
            {
                return;
            }
        }
        protected void abutton_click(object sender, EventArgs e)
        {
            try
            {
                Button te = (Button)sender;
                Label labelid = te.Parent.FindControl(te.ID.Replace("add", "gid")) as Label;
                string sgoodsid = labelid.Text;
                string addcond = " UserID='" + userid + "' and  GoodsID='" + sgoodsid + "'";

                TextBox textbox = te.Parent.FindControl(te.ID.Replace("add", "amount")) as TextBox;  //3 关联，可惜现在在服务端
                LinkButton lb = te.Parent.FindControl(te.ID.Replace("add", "delete")) as LinkButton;
                if (textbox.Text == null)
                {
                    //delete_click(lb, e);
                    return;
                }
                Label sale = te.Parent.FindControl(te.ID.Replace("add", "sm")) as Label;
                if (sale.Text == "1")
                {
                    double a = Double.Parse(textbox.Text) +1;
                    textbox.Text = a + "";
                    Dictionary<string, string> info = new Dictionary<string, string>();
                    info["Amount"] = textbox.Text;
                    sql.UpdateMess("CartInfo", info, addcond);
                }
                else if (sale.Text == "0")
                {
                    double a = Double.Parse(textbox.Text) + 1;
                    textbox.Text = a + "";
                    Dictionary<string, string> info = new Dictionary<string, string>();
                    info["Amount"] = textbox.Text;
                    sql.UpdateMess("CartInfo", info, addcond);
                    Label label2 = te.Parent.FindControl(te.ID.Replace("add", "price")) as Label;
                    if (label2.Text == null)
                    {
                        return;
                    }
                    double p = Double.Parse(label2.Text);
                    Label label3 = te.Parent.FindControl(te.ID.Replace("add", "sum")) as Label;
                    label3.Text = a * p + "";
                    totalprice = Double.Parse(g.Text) + p;
                    g.Text = "" + totalprice;
                }
                else if (sale.Text == "2")
                {
                    double a = Double.Parse(textbox.Text) + 0.1;
                    textbox.Text = a + "";
                    Dictionary<string, string> info = new Dictionary<string, string>();
                    info["Weight"] = textbox.Text;
                    sql.UpdateMess("CartInfo", info, addcond);
                    Label label2 = te.Parent.FindControl(te.ID.Replace("add", "price")) as Label;
                    if (label2.Text == null)
                    {
                        return;
                    }
                    double p = Double.Parse(label2.Text);
                    Label label3 = te.Parent.FindControl(te.ID.Replace("add", "sum")) as Label;
                    totalprice = Double.Parse(g.Text) - Double.Parse(label3.Text) + 0.000001;
                    Double tempsum = formatTrans(a * p);
                    label3.Text = tempsum + "";
                    totalprice += tempsum;
                    g.Text = "" + this.formatTrans(totalprice);
                }


            }
            catch (NullReferenceException ex)
            {
                return;
            }
        }
       
        protected void delete_click(object sender, EventArgs e)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "dialog", "<script>verifydelete();</script>");
            if (f.Text == "") {
                f.Text = flag + "";
            }
            LinkButton te = sender as LinkButton;  
            int id = int.Parse(te.ID.Replace("delete", ""));
            Label label = te.Parent.FindControl(te.ID.Replace("delete", "sm")) as Label;
            if (label.Text == "1")
            {
                f.Text = (Double.Parse(f.Text) - 1) + "";
                if (f.Text == "0")
                {
                    (te.Parent.FindControl("daiding") as Label).Text = "";
                }
                Label label2 = te.Parent.FindControl(te.ID.Replace("delete", "cartid")) as Label;
                //string sgoodsid = label2.Text;
                string deletec = " CartInfo.ID='" + label2.Text + "'";
                sql.SpecialDelete("CartInfo", deletec);
                //te.Parent.Parent.Visible = false;
                Response.Redirect("Cart.aspx");
            }
            else
            {
                try
                {

                    Label label2 = te.Parent.FindControl(te.ID.Replace("delete", "cartid")) as Label;
                    //string sgoodsid = label2.Text;
                    string deletec = " CartInfo.ID='" + label2.Text+"'";
                    sql.SpecialDelete("CartInfo", deletec);
                    Label p = te.Parent.FindControl(te.ID.Replace("delete", "sum")) as Label;
                    g.Text = "" + (Double.Parse(g.Text) - Double.Parse(p.Text));
                  
                    //te.Parent.Parent.Visible = false;
                    Response.Redirect("Cart.aspx");
                } 
                catch (ArgumentOutOfRangeException ex)
                {
                    Response.Write("<script language='javascript'>alert('非常抱歉，操作失败，请刷新页面重试');</script>");
                    return;
                }
                int m = 0;
                if (count.Text == "")
                {
                    m = number - 1;

                }
                else
                {
                    m = Int32.Parse(count.Text) - 1;
                }
                count.Text = m + "";
                if (m == 0)
                {
                    MyTable.Visible = false;
                    notify.Visible = true;
                }
            }
       }


        protected void submit_click(object sender, EventArgs e) 
        {
            Response.Redirect("Order.aspx");
        }
        protected void ImageButton_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton ib = sender as ImageButton;
            Label label2 = ib.Parent.FindControl(ib.ID.Replace("imageb", "gid")) as Label;
            string goodsid = label2.Text;
            Response.Redirect("Detail.aspx?ID="+goodsid);
            //Response.Write("<script>window.open('.aspx','_blank')</script>");
            //Server.Transfer("Detail.aspx?ID=" + goodsid);
        }
        protected void lb_click(object sender, EventArgs e)
        {
            LinkButton ib = sender as LinkButton;
            Label label2 = ib.Parent.FindControl(ib.ID.Replace("goodsname", "gid")) as Label;
            string goodsid = label2.Text;
            Response.Redirect("Detail.aspx?ID="+goodsid);
            //Response.Write("<script>window.open('.aspx','_blank')</script>");
            //Server.Transfer("Detail.aspx?ID=" + goodsid);
            //Response.Redirect("Order.aspx");
        }
        public double formatTrans(double n)
        {
            double r = 0;
            r = Math.Floor(100 * n) * 1.0 / 100.0;
            return r;
        }
    }
}